package wzj.mybatisplusdemo.web;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import wzj.mybatisplusdemo.web.dto.RespEntity;

@RestController
@RequestMapping("/ai")
@CrossOrigin
public class AiController {
    @Autowired
    @Qualifier("chatClientTxt")
    private ChatClient chatClient;
    @PostMapping("/txt")
    public RespEntity aiTxt(String ask){
        String answer = chatClient
                .prompt()
                .user(ask)
                .call()
                .content();
        return new RespEntity("2000","成功",answer);
    }
    @RequestMapping(value = "/img")
    public RespEntity image(String ask){
        int num = 1;
        // 创建RestTemplate实例
        RestTemplate restTemplate = new RestTemplate();
        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("X-DashScope-Async","enable");
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", "Bearer " + System.getenv("DASHSCOPE_API_KEY"));
        String requestBody = """
                    {
                          "model": "wan2.2-t2i-flash",
                          "input": {
                              "prompt": "@p"
                          },
                          "parameters": {
                              "size": "1024*1024",
                              "n": @n
                          }
                """;
        requestBody = requestBody.replace("@p",ask).replace("@n",num+"");

        // 创建请求实体
        HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers);

        // 发送POST请求并返回结果
        ResponseEntity<String> response = restTemplate.postForEntity(
                "https://dashscope.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis",
                requestEntity,
                String.class
        );
        return new RespEntity("2000","成功",response.getBody());
    }
    @RequestMapping(value = "/imgstatus")
    public RespEntity isFinish(String taskId){
        // 创建RestTemplate实例
        RestTemplate restTemplate = new RestTemplate();
        // 创建请求头
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization", "Bearer " + System.getenv("DASHSCOPE_API_KEY"));
        HttpEntity<String> requestEntity = new HttpEntity<>(headers);
        // 发送GET请求并返回结果
        String url="https://dashscope.aliyuncs.com/api/v1/tasks/"+taskId;
        ResponseEntity<String> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                requestEntity,
                String.class
        );
        return new RespEntity("2000","成功",response.getBody());
    }
}
